package com.taotao.protal.service.impl;

import com.taotao.pojo.TaotaoResult;
import com.taotao.pojo.TbContent;
import com.taotao.protal.beans.AdNode;
import com.taotao.protal.service.ContentService;
import com.taotao.protal.utils.HttpClientUtil;
import com.taotao.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 李小磊 on 2017/4/18.
 */
@Service
public class ContestServiceImpl implements ContentService {
    @Value("${REST_BASE_URL}")
    private String REST_BASE_URL;
    @Value("${REST_CONTENT_URL}")
    private String REST_CONTENT_URL;
    @Value("${REST_CONTENT_AD1_CID}")
    private String REST_CONTENT_AD1_CID;

    @Override
    public String getAd1List() {
        String json = HttpClientUtil.doGet(REST_BASE_URL + REST_CONTENT_URL + REST_CONTENT_AD1_CID);
        TaotaoResult result = TaotaoResult.formatToList(json, TbContent.class);
        List<TbContent> list = (List<TbContent>) result.getData();

        if (list == null){
            return "";
        }
        List<AdNode> resultList = new ArrayList<>();
        for (TbContent tbContent : list) {
            AdNode node = new AdNode();
            node.setHeight(240);
            node.setWidth(670);
            node.setSrc(tbContent.getPic());

            node.setHeightB(240);
            node.setWidthB(550);
            node.setSrcB(tbContent.getPic2());

            node.setAlt(tbContent.getSubTitle());
            node.setHref(tbContent.getUrl());

            resultList.add(node);
        }
        String resultJson = JsonUtils.objectToJson(resultList);
        return resultJson;
    }
}
